package com.lovo.bbs.filter;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.lovo.bbs.bo.ForumStatBo;
import com.lovo.bbs.vo.ForumStatVo;
import com.lovo.bbs.vo.TopicVo;
import com.lovo.bbs.bo.TopicBo;

/**
 * 初始化搜索页面的数据
 * @author tiancen2001
 *
 */
public class InitSearchFilter implements Filter{

	public void destroy() {
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest req = (HttpServletRequest)request;
		
		String cond = req.getParameter("cond");//搜索类型
		req.setAttribute("cond", cond);
		String forumid = (String)request.getParameter("fid");//搜索指定论坛信息
		if(forumid==null){
			forumid="0";
		}
		req.setAttribute("fid", forumid);
		
		ServletContext context=req.getSession().getServletContext();
		// 取得网站杂项信息
		ForumStatVo forumInfo = (ForumStatVo)context.getAttribute("forumInfo");
		//更新网站杂项信息
		if(forumInfo==null){
			forumInfo = new ForumStatBo().getForumInfo();
		}
		context.setAttribute("forumInfo", forumInfo);
		req.setAttribute("forumInfo", forumInfo);
		
		
		//搜索
		ArrayList<TopicVo> topicVos = new ArrayList<TopicVo>();
		topicVos = new TopicBo().switchSearch(cond,Integer.parseInt(forumid));
		req.setAttribute("resultCount",topicVos.size() );//搜索结果数
		req.setAttribute("resultList", topicVos);//结果列表
		
		chain.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

}
